package my.service.Impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.utils.DBUtils;
import my.bean.Credits;
import my.dao.ICreditsDao;
import my.dao.Impl.ICreditDaoImpl;
import my.service.ICreditsService;

public class ICreditsServiceImpl implements ICreditsService {
	private ICreditsDao creditDao;

	@Override
	public int add(Credits credit) throws SQLException {
		int record = 0;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			creditDao = new ICreditDaoImpl(conn);
			record = creditDao.add(credit);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("新增失败,尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
				e1.printStackTrace();
			}
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
		return record;
	}

	@Override
	public int modidy(Credits credit) throws SQLException {
		int record = 0;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			creditDao = new ICreditDaoImpl(conn);
			record = creditDao.modidy(credit);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("修改失败,尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
				e1.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int deleteById(int cid) throws SQLException {
		int record = 0;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			creditDao = new ICreditDaoImpl(conn);
			record = creditDao.deleteById(cid);
			conn.commit();
			return record;
		} catch (SQLException e) {
			System.out.println("删除失败,尝试进行事务回滚!");
			try {
				conn.rollback();
				System.out.println("事务回滚成功!");
			} catch (SQLException e1) {
				System.out.println("事务回滚失败!");
				e1.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Credits> findAll() {
		List<Credits> list = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			creditDao = new ICreditDaoImpl(conn);
			list = creditDao.findAll();
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Credits> findByUserID(int userid) {
		List<Credits> list = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			creditDao = new ICreditDaoImpl(conn);
			list = creditDao.findByUserID(userid);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Credits findByCId(int cid) {
		Credits credits = null;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			creditDao = new ICreditDaoImpl(conn);
			credits = creditDao.findByCId(cid);
			return credits;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}
}
